package juc.deadlock;

import lombok.extern.slf4j.Slf4j;

import static juc.Sleeper.sleep;


//活锁案例
//活锁出现在两个线程互相改变对方的结束条件，最后谁也无法结束
@Slf4j(topic = "c.TestLiveLock03")
public class TestLiveLock03 {
    //共同的count变量
    static volatile int count = 10;
    public static void main(String[] args) {
        new Thread(() -> {
            // 期望减到 0 退出循环
            while (count > 0) {
                sleep(0.2);
                count--;
                log.debug("count: {}", count);
            }
        }, "t1").start();
        new Thread(() -> {
            // 期望超过 20 退出循环
            while (count < 20) {
                sleep(0.2);
                count++;
                log.debug("count: {}", count);
            }
        }, "t2").start();
    }
}
